<?php

/*
Process Add Pet (admin_add_pet.pro.php)
*/

$rank_check = "7";
include "../global.inc.php";

$game=$_GET['game'];
$add_pet_name = $_POST['add_pet_name'];
$pet_desc = $_POST['pet_desc'];
$item_likes = $_POST['item_likes'];
$item_dislikes = $_POST['item_dislikes'];
$starter_pet = $_POST['starter_pet'];


$directoryName = "../images/user_images/opg_$game";
if (!file_exists($directoryName)) { mkdir($directoryName, 0777); }

$directoryName2 = "../images/user_images/opg_$game/pets";
if (!file_exists($directoryName2)) { mkdir($directoryName2, 0777); }

if ($HTTP_POST_FILES['petImage']['size'] >= 99999)
{
	die(header(error("admin_add_pet.php?game=$game", "Your image must be under 99,999 bytes (or 99kb)")));
}

if (!eregi("image/", $HTTP_POST_FILES['petImage']['type']))
{
	die(header(error("admin_add_pet.php?game=$game", "You can only use .gif, .jpg, and .jpeg files for images. Please convert the image to one of these file types.")));
}

$check = fetch("SELECT id FROM pets2 WHERE name = '$add_pet_name' AND game = '$game'");

$explodeAttacks = explode(", ", $item_likes);
$countAttacks = count($explodeAttacks) - 1;

if ($countAttacks != "0")
{
for ($a = 0; $a <= $countAttacks;)
{
	$findItem = fetch("SELECT * FROM items2 WHERE item_name = '$explodeAttacks[$a]' AND game = '$game'");
	if (!$findItem[id])
	{
		die(header(error("admin_add_pet.php?game=$game","The item \"$explodeAttacks[$a]\" does not exsist!")));
	}
	$a++;
}
}
$explodeAttacks = explode(", ", $item_dislikes);
$countAttacks = count($explodeAttacks) - 1;
if ($countAttacks != "0")
{
for ($a = 0; $a <= $countAttacks;)
{
	$findItem = fetch("SELECT * FROM items2 WHERE item_name = '$explodeAttacks[$a]' AND game = '$game'");
	if (!$findItem[id])
	{
		die(header(error("admin_add_pet.php?game=$game","The item \"$explodeAttacks[$a]\" does not exsist!")));
	}
	$a++;
}
}
if (!$add_pet_name)
{
	die(header(error("admin_add_pet.php?game=$game", "Please do not leave any information blank!")));
}

if ($check[id])
{
	die(header(error("admin_add_pet.php?game=$game", "There is already a pet with the name $add_pet_name.")));
}
else
{
	mysql_query("INSERT INTO pets2 (name,game,pet_desc,item_likes,item_dislikes,starter) VALUES ('$add_pet_name','$game','$pet_desc','$item_likes','$item_dislikes','$starter_pet')");
	$insert_id = mysql_insert_id();
	
	$file = $HTTP_POST_FILES['petImage']['tmp_name'];
	$dest = "../images/user_images/opg_$game/pets/pet_$insert_id.gif";
	
	copy($file, $dest);
	
	header("Location: admin_add_pet.php?game=$game&error=The+pet+$add_pet_name+has+been+added!");
}

?>
